package com.simpe.boot.demo.controller;

import com.simpe.boot.demo.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisTemplate<Serializable,Object> redisTemplate;

    @GetMapping(value = "str")
    public void insertString() {
        redisTemplate.opsForValue().set("str", "stringValue");
    }
    @GetMapping(value = "find/str")
    public Object findString() {
        return redisTemplate.opsForValue().get("str");
    }


    @GetMapping(value = "object")
    public void insertStringObject() {
        UserInfo user = new UserInfo();
        user.setUserId(1);
        user.setUsername("user1");
        user.setPassword("password1");
        redisTemplate.opsForValue().set("obj", user);
    }

    @GetMapping(value = "find/object")
    public Object findObject() {
        return redisTemplate.opsForValue().get("obj");
    }
}